import Vue from 'vue'
import Router from 'vue-router'
import Index from '@/components/index'
import GoodsList from '@/components/goodsList'
import GoodsView from '@/components/goodsView'
import Login from '@/components/login'
import Cart from '@/components/cart'
import OrderConfirm from '@/components/orderConfirm'
import Pay from '@/components/pay'
import Merchants from '@/components/merchants'
import GoodsManage from '@/components/goodsManage'
import KillManage from '@/components/killManage'
import Personal from '@/components/personal'
import KillView from '@/components/killView'
import KillConfirm from '@/components/killConfirm'
import CookieUtil from '../util/CookieUtil'
import Search from '@/components/search'

Vue.use(Router)

let router =new Router({
  routes: [
    {
      path: '/',
      name: 'Index',
      component: Index
    },
    {
      path: '/goodsList/:cid',   // 动态路由：带参数的路由
      name: 'GoodsList',
      component: GoodsList
    },
    {
      path: '/goodsView/:gid',   // 动态路由：带参数的路由
      name: 'GoodsView',
      component: GoodsView
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/cart',
      name: 'Cart',
      component: Cart
    },
    {
      path: '/orderConfirm',
      name: 'OrderConfirm',
      component: OrderConfirm
    },
    {
      path: '/pay',
      name: 'Pay',
      component: Pay
    },
    {
      path: '/merchants',
      name: 'Merchants',
      component: Merchants,

      children:[
        {
          path:"/",
          // component: GoodsManage
          redirect: "goodsManage"
        },
        {
          path:"goodsManage",  // 二级路由的path不能以/开头  规定
          name:"GoodsManage",
          component: GoodsManage
        },
        {
          path:"killManage",  // 二级路由的path不能以/开头  规定
          name:"KillManage",
          component: KillManage
        },
        {
          path:"personal",  // 二级路由的path不能以/开头  规定
          name:"Personal",
          component: Personal
        }
      ]
    },
    {
      path:"/killView/:kid",
      name:"KillView",
      component: KillView
    },
    {
      path:"/killConfirm/:orderno",
      name:"KillConfirm",
      component: KillConfirm
    },
    {
      path:"/search/:content",
      name:"Search",
      component: Search
    },
  ],
  mode: "history"   // 指定路由的工作模式   history不带#号
})

//配置守卫路由
router.beforeEach((to,from,next)=>{
  console.log(to.path,from.path)

//1.没登录能直接访问的
let permitAll = ['/login','goodsList','/goodsView','/search/']

//2.登录了但是没权限访问的
let perms = {
  consumer:['/cart','/order','/orderConfirm','/pay','/killView/'],
  merchants:['/merchants/']
}
//根路径 直接放行
if(to.path =='/'){
  next()
  return
}

//如果是不需要认证的直接放行
for (const permit of permitAll) {
  if(to.path == permit ||to.path.startsWith(permit)){
    next()
    return
  }
}
//判断是否登录 cookie
if(!CookieUtil.get("authorization")){
//没登录
next("/login")
return
}
//判断用户角色
//获取用户有哪些角色
let user = JSON.parse(localStorage.getItem("user"))
let roles = user.roleList;
for (const role of roles) {
  if(role.name=='consumer'){
   for (const url of perms.consumer) {
    if(to.path== url || to.path.startsWith(url)){
      //相同放行
      next()
      return
    }
   }
  }
  if(role.name=='merchants'){
    for (const url of perms.merchants) {
      if(to.path== url || to.path.startsWith(url)){
        //相同放行
        next()
        return
      }
     }
  }
}
//for循环执行完毕还是没放行,说明to就不应该是当前用户能去的地方
next("/")
})


export default router
